<?php
/**
 * Object Relational Persistence Mapping Class for table: calendar_event
 *
 * @package	com.athena
 * @author	LHV
 */
class CalendarEvent extends ORDataObject {

	/**#@+
	 * Fields of table: calendar_event mapped to class members
	 */
	var $calendar_event_id		= '';
	var $creator_id		= '';
	var $start_date		= '';
	var $end_date		= '';
	var $start_time		= '';
	var $end_time		= '';
	var $name			= '';
	var $description	= '';
	var $recur_type 	= '';
	var $recur_data 	= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'calendar_event';

	/**
	 * Primary Key
	 */
	var $_key = 'calendar_event_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'CalendarEvent';

	/**
	 * Handle instantiation
	 */
	function CalendarEvent() {
		parent::ORDataObject();
	}

	
	/**#@+
	 * Field: start_date, time formatting
	 */
	function get_start_date() {
		return $this->_getDate('start_date');
	}
	function set_start_date($date) {
		$this->_setDate('start_date',$date);
	}
	/**#@-*/

	/**#@+
	 * Field: end_date, time formatting
	 */
	function get_end_date() {
		return $this->_getDate('end_date');
	}
	function set_end_date($date) {
		$this->_setDate('end_date',$date);
	}
	/**#@-*/
	
	public function getEvents($userId, $date, $forView = 'month') {
		$isoDate = &new DateObject();
		$isoDate = $isoDate->createFromVN($date);
		$day_text = date("D", mktime(0,0,0, $isoDate->getMonth(), $isoDate->getDay(), $isoDate->getYear()) );	
		$isoDate = $isoDate->toISO();
		$mask_bits = '';
		switch($day_text){
			case 'Sun':
				$mask_bits = '0000001';
				break;
			case 'Sat':
				$mask_bits = '0000010';
				break;
			case 'Mon':
				$mask_bits = '1000000';
				break;
			case 'Tue':
				$mask_bits = '0100000';
				break;
			case 'Wed':
				$mask_bits = '0010000';
				break;
			case 'Thu':
				$mask_bits = '0001000';
				break;
			case 'Fri':
				$mask_bits = '0000100';
				break;	
		}	
		$ds = new Datasource_sql();
		$cols = "ce.calendar_event_id, ce.start_time, ce.name";
		if ($forView == 'week') {
			$cols .= ", ce.end_time";
		}
		if($forView == 'day'){
			$cols .= ", ce.end_time ,ce.description"; 
		}
		$sql = array (
			'cols' 		=> $cols,
			'from'		=> "$this->_table ce LEFT JOIN calendar_event_map cem ON (ce.calendar_event_id = cem.calendar_event_id)",
			'where'		=> "date(ce.start_date) <= '$isoDate' AND date(ce.end_date) >= '$isoDate' AND cem.person_id = $userId
							AND (recur_data & b'$mask_bits')",					
			'orderby'	=> "ce.start_time"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function AddEvent($eventId, $name, $description, $start_date, 
							 $end_date, $start_time, $end_time, $creator_id, $recur_type, $recur_data){
		
		echo $recur_data;
		$data = array(
			'name' 			=> $name,
			'description'	=> $description,
			'start_date' 	=> $start_date,
			'end_date' 		=> $end_date,
			'start_time'	=> $start_time,
			'end_time'		=> $end_time,
			'creator_id'	=> $creator_id,
			'recur_type'	=> $recur_type
			);
		if(isset($eventId) && $eventId != "" ){
			$this->setup($eventId);
		}
		$this->populateArray($data);
		$this->persist();
		$new_event_id = $this->calendar_event_id;
		$sql = "UPDATE $this->_table 
				SET recur_data = b'$recur_data'
				WHERE calendar_event_id = $new_event_id";
		$this->_execute($sql);
		return $new_event_id;		
	}
	
	public function getAllEvents(){
		$ds = new Datasource_sql();
		$sql = array (
			'cols' 		=> "calendar_event_id, name, description, 
							DATE_FORMAT(start_date, '%d/%m/%Y') start_date, 
							DATE_FORMAT(end_date,'%d/%m/%Y') end_date, 
							TIME_FORMAT(start_time,'%H:%i') start_time, 
							TIME_FORMAT(end_time,'%H:%i') end_time",
			'from'		=> "$this->_table",
			'where'		=> "date(end_date) >= date(now()) " ,					
			'orderby'	=> "name"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function getEvent($eventId){
		$ds = new Datasource_sql();
		$sql = array (
			'cols' 		=> "calendar_event_id, name, description, 
							DATE_FORMAT(start_date, '%d/%m/%Y') start_date, 
							DATE_FORMAT(end_date,'%d/%m/%Y') end_date, 
							TIME_FORMAT(start_time,'%H') start_hour, 
							TIME_FORMAT(start_time,'%i') start_minute,
							TIME_FORMAT(end_time,'%H') end_hour,
							TIME_FORMAT(end_time,'%i') end_minute",
			'from'		=> "$this->_table",
			'where'		=> "calendar_event_id = $eventId " 
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function deleteEvent($eventId){
		$sql = " DELETE FROM $this->_table WHERE calendar_event_id = $eventId";
		$this->_execute($sql);
		return ;
	}
}
?>
